package com.yincheng.oes.business.controller;

import com.yincheng.oes.business.model.dto.StudentDailyClockDTO;
import com.yincheng.oes.business.model.query.AuditDailyClockListReq;
import com.yincheng.oes.business.model.query.MyDailyClockListReq;
import com.yincheng.oes.business.model.result.AuditDailyClockListResult;
import com.yincheng.oes.business.model.result.BusinessResult;
import com.yincheng.oes.business.model.result.MyDailyClockListResult;
import com.yincheng.oes.business.service.impl.DailyClockServiceImpl;
import com.yincheng.oes.business.util.Tools;
import com.yincheng.oes.business.util.book.dto.BookDTO;
import com.yincheng.oes.business.util.book.service.impl.BookServiceImpl;
import com.yincheng.oes.sso.entity.User;
import com.yincheng.oes.sso.service.UserService;
import com.yincheng.oes.sso.util.UserUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 每日打卡controller
 */
@RequestMapping("/dailyClock")
@Controller
public class DailyClockController {

    @Autowired
    private DailyClockServiceImpl dailyClockService;

    @Autowired
    private BookServiceImpl bookService;

    @Autowired
    private UserService userService;

    /**
     * 获取我的每日打卡列表
     *
     * @param param 查询条件
     * @return 响应
     */
    @RequestMapping(value = "/getList.json", method = RequestMethod.GET)
    @ResponseBody
    public String list(MyDailyClockListReq param) {
        Pair<Integer, List<MyDailyClockListResult>> pair = dailyClockService.getMyDailyClockList(param);
        return Tools.toEasyuiDataGrid(pair.getLeft(), pair.getRight());
    }

    /**
     * 获取每日打卡审核列表
     *
     * @param param 入参
     * @return 列表
     */
    @RequestMapping(value = "/getAuditList.json", method = RequestMethod.GET)
    @ResponseBody
    public String auditList(AuditDailyClockListReq param) {
        Pair<Integer, List<AuditDailyClockListResult>> pair = dailyClockService.getAuditDailyClockList(param);
        return Tools.toEasyuiDataGrid(pair.getLeft(), pair.getRight());
    }

    @RequestMapping(value = "/auditDailyClockList.html", method = RequestMethod.GET)
    public ModelAndView auditDailyClockList(ModelAndView view) {
        List<User> users = userService.searchByAssistantId(UserUtils.getUser().getUserId());
        view.addObject("students", users);
        view.setViewName("business/dailyclock/auditdailyclocklist");
        return view;
    }

    @RequestMapping(value = "/myDailyClockList.html", method = RequestMethod.GET)
    public String myDailyClockList() {
        return "business/dailyclock/mydailyclocklist";
    }

    /**
     * 检查当前是否重复每日打卡
     *
     * @return BusinessResult code=0 可以打卡
     */
    @RequestMapping(value = "/checkDailyClock.json", method = RequestMethod.POST)
    @ResponseBody
    public BusinessResult checkDailyClock() {
        return dailyClockService.checkDailyClock();
    }

    /**
     * 打开我的打卡界面
     *
     * @return dailyClock path
     */
    @RequestMapping(value = "/openDailyClock.html", method = RequestMethod.GET)
    public ModelAndView openDailyClock(ModelAndView view) {
        StudentDailyClockDTO dailyClock = dailyClockService.getDailyClock();
        view.addObject("form", dailyClock);
        List<BookDTO> books = bookService.selectAll();
        view.addObject("books", books);
        view.setViewName("business/dailyclock/mydailyclock-edit");
        return view;
    }

    @RequestMapping(value = "/openReadDailyClock.html", method = RequestMethod.GET)
    public ModelAndView openReadDailyClock(ModelAndView view, String dailyClockId) {
        StudentDailyClockDTO dailyClock = dailyClockService.getAuditDailyClock(dailyClockId);
        view.addObject("form", dailyClock);
        List<BookDTO> books = bookService.selectAll();
        view.addObject("books", books);
        view.setViewName("business/dailyclock/mydailyclock-read");
        return view;
    }

    @RequestMapping(value = "/openAuditDailyClock.html", method = RequestMethod.GET)
    public ModelAndView openAuditDailyClock(ModelAndView view, String dailyClockId) {
        StudentDailyClockDTO dailyClock = dailyClockService.getAuditDailyClock(dailyClockId);
        view.addObject("form", dailyClock);
        List<BookDTO> books = bookService.selectAll();
        view.addObject("books", books);
        view.setViewName("business/dailyclock/dailyclock-audit");
        return view;
    }

    @RequestMapping(value = "/doDailyClock.json", method = RequestMethod.POST)
    @ResponseBody
    public BusinessResult doDailyClock(StudentDailyClockDTO studentDailyClockDTO) {
        return dailyClockService.doDailyClock(studentDailyClockDTO);
    }

    @RequestMapping(value = "/auditDailyClock.json", method = RequestMethod.POST)
    @ResponseBody
    public BusinessResult auditDailyClock(StudentDailyClockDTO studentDailyClockDTO) {
        return dailyClockService.doAuditDailyClock(studentDailyClockDTO);
    }

    @RequestMapping(value = "/openRadar.html", method = RequestMethod.GET)
    public ModelAndView openRadar(ModelAndView view) {
//        StudentDailyClockDTO dailyClock = dailyClockService.getDailyClock();
//        view.addObject("form", dailyClock);
//        List<BookDTO> books = bookService.selectAll();
//        view.addObject("books", books);
        view.setViewName("business/dailyclock/myRadar");
        return view;
    }

    /**
     * 手动触发 计算学员上一周的学力雷达图
     *
     * @return BusinessResult
     */
    @RequestMapping(value = "/doCalStudentRadar.json", method = RequestMethod.POST)
    public BusinessResult doCalStudentRadar() {


        return null;
    }


}
